{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c3409944",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-16\n",
    "* 课程：Python语言\n",
    "* week06\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9a212ea2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时钟的分钟是偶数\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [  1, 3,  5,  7,  9, 11,13,15,17,19,\n",
    "             21,23,25,27,29,31,33,35,37,39,\n",
    "             41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"当前时钟的分钟为奇数\")\n",
    "else:\n",
    "    print(\"当前时钟的分钟是偶数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0b3de6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7e829e7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2023"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.today().year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7cb2badd",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on datetime object:\n",
      "\n",
      "class datetime(date)\n",
      " |  datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])\n",
      " |  \n",
      " |  The year, month and day arguments are required. tzinfo may be None, or an\n",
      " |  instance of a tzinfo subclass. The remaining arguments may be ints.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      datetime\n",
      " |      date\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __radd__(self, value, /)\n",
      " |      Return value+self.\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      __reduce__() -> (cls, state)\n",
      " |  \n",
      " |  __reduce_ex__(...)\n",
      " |      __reduce_ex__(proto) -> (cls, state)\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  __rsub__(self, value, /)\n",
      " |      Return value-self.\n",
      " |  \n",
      " |  __str__(self, /)\n",
      " |      Return str(self).\n",
      " |  \n",
      " |  __sub__(self, value, /)\n",
      " |      Return self-value.\n",
      " |  \n",
      " |  astimezone(...)\n",
      " |      tz -> convert to local time in new timezone tz\n",
      " |  \n",
      " |  ctime(...)\n",
      " |      Return ctime() style string.\n",
      " |  \n",
      " |  date(...)\n",
      " |      Return date object with same year, month and day.\n",
      " |  \n",
      " |  dst(...)\n",
      " |      Return self.tzinfo.dst(self).\n",
      " |  \n",
      " |  isoformat(...)\n",
      " |      [sep] -> string in ISO 8601 format, YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].\n",
      " |      sep is used to separate the year from the time, and defaults to 'T'.\n",
      " |      The optional argument timespec specifies the number of additional terms\n",
      " |      of the time to include. Valid options are 'auto', 'hours', 'minutes',\n",
      " |      'seconds', 'milliseconds' and 'microseconds'.\n",
      " |  \n",
      " |  replace(...)\n",
      " |      Return datetime with new specified fields.\n",
      " |  \n",
      " |  time(...)\n",
      " |      Return time object with same time but with tzinfo=None.\n",
      " |  \n",
      " |  timestamp(...)\n",
      " |      Return POSIX timestamp as float.\n",
      " |  \n",
      " |  timetuple(...)\n",
      " |      Return time tuple, compatible with time.localtime().\n",
      " |  \n",
      " |  timetz(...)\n",
      " |      Return time object with same time and tzinfo.\n",
      " |  \n",
      " |  tzname(...)\n",
      " |      Return self.tzinfo.tzname(self).\n",
      " |  \n",
      " |  utcoffset(...)\n",
      " |      Return self.tzinfo.utcoffset(self).\n",
      " |  \n",
      " |  utctimetuple(...)\n",
      " |      Return UTC time tuple, compatible with time.localtime().\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods defined here:\n",
      " |  \n",
      " |  combine(...) from builtins.type\n",
      " |      date, time -> datetime with same date and time fields\n",
      " |  \n",
      " |  fromisoformat(...) from builtins.type\n",
      " |      string -> datetime from datetime.isoformat() output\n",
      " |  \n",
      " |  fromtimestamp(...) from builtins.type\n",
      " |      timestamp[, tz] -> tz's local time from POSIX timestamp.\n",
      " |  \n",
      " |  now(tz=None) from builtins.type\n",
      " |      Returns new datetime object representing current time local to tz.\n",
      " |      \n",
      " |        tz\n",
      " |          Timezone object.\n",
      " |      \n",
      " |      If no tz is specified, uses local timezone.\n",
      " |  \n",
      " |  strptime(...) from builtins.type\n",
      " |      string, format -> new datetime parsed from a string (like time.strptime()).\n",
      " |  \n",
      " |  utcfromtimestamp(...) from builtins.type\n",
      " |      Construct a naive UTC datetime from a POSIX timestamp.\n",
      " |  \n",
      " |  utcnow(...) from builtins.type\n",
      " |      Return a new datetime representing UTC day and time.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  fold\n",
      " |  \n",
      " |  hour\n",
      " |  \n",
      " |  microsecond\n",
      " |  \n",
      " |  minute\n",
      " |  \n",
      " |  second\n",
      " |  \n",
      " |  tzinfo\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  max = datetime.datetime(9999, 12, 31, 23, 59, 59, 999999)\n",
      " |  \n",
      " |  min = datetime.datetime(1, 1, 1, 0, 0)\n",
      " |  \n",
      " |  resolution = datetime.timedelta(microseconds=1)\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from date:\n",
      " |  \n",
      " |  __format__(...)\n",
      " |      Formats self with strftime.\n",
      " |  \n",
      " |  isocalendar(...)\n",
      " |      Return a 3-tuple containing ISO year, week number, and weekday.\n",
      " |  \n",
      " |  isoweekday(...)\n",
      " |      Return the day of the week represented by the date.\n",
      " |      Monday == 1 ... Sunday == 7\n",
      " |  \n",
      " |  strftime(...)\n",
      " |      format -> strftime() style string.\n",
      " |  \n",
      " |  toordinal(...)\n",
      " |      Return proleptic Gregorian ordinal.  January 1 of year 1 is day 1.\n",
      " |  \n",
      " |  weekday(...)\n",
      " |      Return the day of the week represented by the date.\n",
      " |      Monday == 0 ... Sunday == 6\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from date:\n",
      " |  \n",
      " |  fromisocalendar(...) from builtins.type\n",
      " |      int, int, int -> Construct a date from the ISO year, week number and weekday.\n",
      " |      \n",
      " |      This is the inverse of the date.isocalendar() function\n",
      " |  \n",
      " |  fromordinal(...) from builtins.type\n",
      " |      int -> date corresponding to a proleptic Gregorian ordinal.\n",
      " |  \n",
      " |  today(...) from builtins.type\n",
      " |      Current date or datetime:  same as self.__class__.fromtimestamp(time.time()).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors inherited from date:\n",
      " |  \n",
      " |  day\n",
      " |  \n",
      " |  month\n",
      " |  \n",
      " |  year\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(datetime.datetime.today())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e6a4efd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 补充random模块的介绍\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b7f52080",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "54\n",
      "95\n",
      "7\n",
      "15\n",
      "45\n",
      "62\n",
      "40\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "# 如果8次数值依次为：[45,34,1,78,61,34,25,23]\n",
    "for i in range(8):\n",
    "    print(int(random.random()*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7f2987ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on method randint in module random:\n",
      "\n",
      "randint(a, b) method of random.Random instance\n",
      "    Return random integer in range [a, b], including both end points.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(random.randint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "41d10d13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0b1c5df4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "2\n",
      "5\n",
      "6\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7223cad4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时钟的秒数为 24,时间将静止 2秒，此刻为偶数时间\n",
      "当前时钟的秒数为 26,时间将静止 3秒，此刻为偶数时间\n",
      "当前时钟的秒数为 29,时间将静止 3秒，此刻为奇数时间\n",
      "当前时钟的秒数为 32,时间将静止 4秒，此刻为偶数时间\n",
      "当前时钟的秒数为 36,时间将静止 3秒，此刻为偶数时间\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(5): # for + range 可以决定程序执行的 次数\n",
    "    right_this_second = datetime.today().second # 选择当前时钟的秒数\n",
    "    wait_time = random.randint(1,5) # 随机选择1-5之间的一个整数\n",
    "    if right_this_second in odds: # 如果当前的秒数为奇数则执行以下代码\n",
    "        print(\"当前时钟的秒数为 %s,时间将静止 %s秒，此刻为奇数时间\"%(right_this_second,wait_time))\n",
    "    else: # 如果当前的秒数为偶数则执行以下代码\n",
    "        print(\"当前时钟的秒数为 %s,时间将静止 %s秒，此刻为偶数时间\"%(right_this_second,wait_time))\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "909aad4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 项目三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4f06cef2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99 bottles 在啤酒墙上~\n",
      "99 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "93 bottles 在啤酒墙上~\n",
      "93 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "87 bottles 在啤酒墙上~\n",
      "87 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "81 bottles 在啤酒墙上~\n",
      "81 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "75 bottles 在啤酒墙上~\n",
      "75 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "69 bottles 在啤酒墙上~\n",
      "69 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "63 bottles 在啤酒墙上~\n",
      "63 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "57 bottles 在啤酒墙上~\n",
      "57 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "51 bottles 在啤酒墙上~\n",
      "51 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "45 bottles 在啤酒墙上~\n",
      "45 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "39 bottles 在啤酒墙上~\n",
      "39 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "33 bottles 在啤酒墙上~\n",
      "33 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "27 bottles 在啤酒墙上~\n",
      "27 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "21 bottles 在啤酒墙上~\n",
      "21 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "15 bottles 在啤酒墙上~\n",
      "15 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "\n",
      "9 bottles 在啤酒墙上~\n",
      "9 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "只剩3瓶了，不能拿了~\n",
      "\n",
      "3 bottles 在啤酒墙上~\n",
      "3 bottles 啤酒~\n",
      "一次6瓶酒~\n",
      "取走一次\n",
      "没了没了，没有酒啦~\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for beer_num in range(99, 0, -6):\n",
    "    print(beer_num,\"bottles\",\"在啤酒墙上~\")\n",
    "    print(beer_num,\"bottles\",\"啤酒~\")\n",
    "    print(\"一次6瓶酒~\")\n",
    "    print(\"取走一次\")\n",
    "    if beer_num == 3:\n",
    "        print(\"没了没了，没有酒啦~\")\n",
    "    else:\n",
    "        new_num = beer_num - 6\n",
    "        if new_num == 3:\n",
    "            print(\"只剩3瓶了，不能拿了~\")\n",
    "    print()\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "996a5d4b",
   "metadata": {},
   "source": [
    "# while 循环\n",
    "\n",
    "## while True 无限循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6ae468c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙,缠缠绵绵到天涯... 1\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 2\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 3\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 4\n",
      "你是风儿我是沙,缠缠绵绵到天涯... 5\n"
     ]
    }
   ],
   "source": [
    "count = 0 # 计数器\n",
    "while True:\n",
    "    count +=1\n",
    "    print(\"你是风儿我是沙,缠缠绵绵到天涯...\",count)\n",
    "    if count == 5:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f235274d",
   "metadata": {},
   "source": [
    "* 笔记：\n",
    "> 1. while循环往往和 True一起使用\n",
    "> 2. while True 如果没有给定确定的条件，将无限循环下去\n",
    "> 3. while True 可以按照一定的条件执行break程序退出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "444c3ab2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "你有4次机会来玩这个游戏\n",
      "请输入你猜的数字：50\n",
      "往大了猜！\n",
      "你有3次机会来玩这个游戏\n",
      "请输入你猜的数字：88\n",
      "你真棒！猜对了\n"
     ]
    }
   ],
   "source": [
    "## 猜数字\n",
    "import getpass\n",
    "\n",
    "# 1. 拿数字给别人猜\n",
    "hide_card  = getpass.getpass(\"hide_card:\")\n",
    "\n",
    "# 2. 设置猜游戏的次数\n",
    "count = 5\n",
    "\n",
    "while True:\n",
    "    count -=1\n",
    "    # 2. 猜数字的人来猜\n",
    "    print(\"你有%s次机会来玩这个游戏\"%(count))\n",
    "    if count == 0:\n",
    "        print(\"游戏失败！\")\n",
    "        break\n",
    "    else:\n",
    "        guess_digit = input(\"请输入你猜的数字：\")  \n",
    "        # 情景一：猜对了\n",
    "        if hide_card == guess_digit:\n",
    "            print(\"你真棒！猜对了\")\n",
    "            break\n",
    "        # 情景二：猜错了\n",
    "        else:\n",
    "            if hide_card > guess_digit:\n",
    "                print(\"往大了猜！\")\n",
    "            elif  hide_card < guess_digit:\n",
    "                print(\"数值比你的数小！\")\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2795d3c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
